﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using POSSystem.BLL;
using System.IO;
using Microsoft.Reporting.WebForms;

namespace WebUI.Report
{
    public partial class SaleReport : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (ReportFrom == null || ReportTo == null)
            {
                ReportFrom = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                ReportTo = DateTime.UtcNow.AddHours(SysVars.TimeZone);
            }
        }

        DateTime ReportFrom
        {
            get 
            {
                if (txtFrom.Text == "")
                    txtFrom.Text = DateTime.UtcNow.AddHours(SysVars.TimeZone).ToString(calTo.Format);
                return DateTime.ParseExact(txtFrom.Text, calFrom.Format, null);
            }
            set { txtFrom.Text = value.ToString(calTo.Format); }
        }

        DateTime ReportTo
        {
            get
            {
                if (txtTo.Text == "")
                    txtTo.Text = DateTime.UtcNow.AddHours(SysVars.TimeZone).ToString(calTo.Format);
                return DateTime.ParseExact(txtTo.Text, calFrom.Format, null) ;
            }
            set { txtTo.Text = value.ToString(calTo.Format); }
        }

        protected void btnGenerate_OnClick(object sender, EventArgs e)
        {
            DateTime from = DateTime.ParseExact(txtFrom.Text, calFrom.Format, null);
            DateTime to = DateTime.ParseExact(txtTo.Text, calTo.Format, null);
            to = to.AddDays(1).AddSeconds(-1);

            CommonSaleReport rptData = new CommonSaleReport();
            rptData.From = from;
            rptData.To = to;
            rptData.BranchID = Branches.CurrentBranchID;
            ReportFrom = from;
            ReportTo = to;
            rptData.IsRetail = rbRetail.Checked;
            rptData.LoadReport(string.Format("Rpt{0}", ddlReport.SelectedValue));

            ReportDataSource rptDataSource = new ReportDataSource(ddlReport.SelectedValue, rptData.DefaultView);

            LoadReport(rptDataSource);
        }

        private void AddParms()
        {
            String tmp = rbRetail.Checked ? " P/L - F" : " P/L - B";
            ReportParameter parm = new ReportParameter("CompanyName", Branches.CurrentBranch.s_Name);
            rptViewer.LocalReport.SetParameters(parm);
            parm = new ReportParameter("CompanyAddress", Branches.CurrentBranch.s_Address);
            rptViewer.LocalReport.SetParameters(parm);
            parm = new ReportParameter("CompanyABN", Branches.CurrentBranch.s_ABN);
            rptViewer.LocalReport.SetParameters(parm);
            parm = new ReportParameter("CompanyPhone", Branches.CurrentBranch.s_Phone);
            rptViewer.LocalReport.SetParameters(parm);
            parm = new ReportParameter("CompanyFax", Branches.CurrentBranch.s_Fax);
            rptViewer.LocalReport.SetParameters(parm);
            parm = new ReportParameter("from", txtFrom.Text);
            rptViewer.LocalReport.SetParameters(parm);
            parm = new ReportParameter("to", txtTo.Text);
            rptViewer.LocalReport.SetParameters(parm);
            parm = new ReportParameter("invoiceUrl", Utilities.GetConfigAppSetting("InvoiceUrl"));
            rptViewer.LocalReport.SetParameters(parm);
        }

        private void LoadReport(ReportDataSource ds)
        {
            String rptFile = Server.MapPath(string.Format("~/Report/{0}.rdlc", ddlReport.SelectedValue));
            if (!File.Exists(rptFile))
            {
                lalbelHint.Text = string.Format("Report File {0}.rdlc does not exist!", ddlReport.SelectedValue);
                lalbelHint.Visible = true;
                rptViewer.Visible = false;
                return;
            }

            rptViewer.LocalReport.DataSources.Clear();
            rptViewer.Visible = true;
            rptViewer.ProcessingMode = ProcessingMode.Local;
            rptViewer.LocalReport.ReportPath = rptFile;
            AddParms();
            rptViewer.LocalReport.DataSources.Add(ds);
            lalbelHint.Visible = false;
        }
    }
}